﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Core.Mapping;
using System.Data.Entity.Core.Metadata.Edm;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WinnerCMS.Common;
using WinnerCMS.Model;

namespace WinnerCMS.Data
{
    /// <summary>
    /// 数据访问层CMS_Template。
    /// </summary>
    public class TemplatesDal
    {

        #region  添加数据
        /// <summary>
        /// 添加数据
        /// </summary>
        public void Add(Template model)
        {
            using (Entities db = new Entities())
            {
                db.Templates.Add(model);
                db.SaveChangesAsync().Wait();
            }
        }
        #endregion

        #region  修改数据
        /// <summary>
        /// 修改
        /// </summary>
        public void Edit(Template model)
        {
            using (Entities db = new Entities())
            {
                db.Templates.Attach(model);
                db.Entry(model).State = EntityState.Modified;
                db.SaveChangesAsync().Wait();
            }
        }
        #endregion

        #region  删除
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="Id"></param>
        public void Delete(string Id)
        {
            using (Entities db = new Entities())
            {
                var q = db.Templates.First(x => x.Id == Id);
                db.Templates.Remove(q);
                db.SaveChangesAsync().Wait();
            }
        }
        #endregion

        #region 应用模板
        /// <summary>
        /// 应用模板
        /// </summary>
        /// <param name="id"></param>
        public void UsedIt(string id)
        {
            using (Entities db = new Entities())
            {
                var q = db.Templates.FirstOrDefault(x => x.Id != id);
                if (q != null)
                {
                    q.IsUsed = false;
                    db.SaveChangesAsync().Wait();
                }

                q = db.Templates.FirstOrDefault(x => x.Id == id);
                q.IsUsed = true;
                db.SaveChangesAsync().Wait();
            }
        }
        #endregion

        #region  加载数据
        /// <summary>
        /// 加载数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Template GetInfo(string id)
        {
            using (Entities db = new Entities())
            {
                return db.Templates.FirstOrDefault(x => x.Id == id);
            }
        }
        #endregion

        #region 取得正在使用的模板

        /// <summary>
        /// 取得正在使用的模板
        /// </summary>
        /// <returns></returns>
        public async Task<Template> GetUseing()
        {
            using (Entities db = new Entities())
            {
                //var objectContext = ((IObjectContextAdapter)db).ObjectContext;
                //var mappingCollection = (StorageMappingItemCollection)objectContext.MetadataWorkspace
                //                                                                    .GetItemCollection(DataSpace.CSSpace);

                //List<EdmSchemaError> errors = new List<EdmSchemaError>();
                //mappingCollection.GenerateViews(errors);

                //mappingCollection.ComputeMappingHashValue();
                db.Configuration.ProxyCreationEnabled = false;
                return await db.Templates.AsNoTracking().FirstAsync(x => x.IsUsed);
            }
        }
        #endregion

        #region 绑定数据
        /// <summary>
        /// 绑定数据
        /// </summary>
        /// <returns></returns>
        public IList<Template> BindList()
        {
            using (Entities db = new Entities())
            {
                return db.Templates.OrderByDescending(x => x.IsUsed).ToList();
            }
        }
        #endregion
    }
}